package org.yonggan.web;


import com.google.common.cache.Cache;
import jdk.nashorn.internal.ir.ReturnNode;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import com.google.common.cache.CacheBuilder;
import sun.util.calendar.BaseCalendar;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

@EnableWebMvc
@Configuration
@Controller
public class WebPageCache {

    /**
     * http缓存
     * @return
     */
    @RequestMapping("/cache")
    public ResponseEntity<String> cache (
            @RequestHeader (value = "If-Modified-Since",required = false) Date ifModifiedSince ) throws Exception {
        DateFormat gmtDateformat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss 'GMT'", Locale.US);

        //当前系统时间
        long now = System.currentTimeMillis() / 1000 * 1000;
        //文档可以在浏览器端/proxy上缓存多久
        long maxAge = 20;

        Long lastModifiedMillis = getLastModified()/ 1000 * 1000;
        //判断内容是否修改了，此处使用等值判断
        if(ifModifiedSince != null && ifModifiedSince.getTime() == lastModifiedMillis) {
            MultiValueMap<String ,String > headers = new HttpHeaders();
            // 当前时间
            headers.add("Date",gmtDateformat.format(new Date(now)));
            // 过期时间 http 1.0支持
            headers.add("Expires", gmtDateformat.format(new Date(now + maxAge*1000)));
            //文档生存时间 http 1.1支持
            headers.add("Cache-Control", "max-age=" + maxAge);
            return new ResponseEntity<String>(headers,HttpStatus.NOT_MODIFIED);
        }

        String body = "<a href=''>点击访问当前链接</a>";
        MultiValueMap<String ,String > headers = new HttpHeaders();
        // 当前时间
        headers.add("Date",gmtDateformat.format(new Date(now)));
        // 过期时间 http 1.0支持
        headers.add("Expires", gmtDateformat.format(new Date(now + maxAge * 1000)));
        //文档生存时间 http 1.1支持
        headers.add("Cache-Control", "max-age=" + maxAge);
        headers.add("Last-Modified",gmtDateformat.format(new Date(now)));
        return new ResponseEntity<String>(headers,HttpStatus.OK);
    }


    Cache<String ,Long> lastModifiedCache   = CacheBuilder.newBuilder()
            .expireAfterWrite(10, TimeUnit.SECONDS).build();

    public long getLastModified () throws ExecutionException {
      // return   lastModifiedCache.get("lastModified",()->{return System.currentTimeMillis();});
        return   lastModifiedCache.get("lastModified", System::currentTimeMillis);
    }


    public ResponseEntity<String> cache (@RequestHeader(value = "",required = false) String ifNoneMatch) {


        return  null;
    }

}
